package com.app.asktom;

import com.app.asktom.component.DeviceHelper;
import com.app.asktom.component.ImageLoaderGoogleMap;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesClient;
import com.google.android.gms.location.LocationClient;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.GoogleMap.OnMapClickListener;
import com.google.android.gms.maps.GoogleMap.OnMapLongClickListener;
import com.google.android.gms.maps.model.LatLng;

import android.location.Location;
import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.IntentSender;
import android.view.Menu;
import android.widget.Toast;

public class MapCoordinateActivity extends Activity implements OnMapClickListener, OnMapLongClickListener, GooglePlayServicesClient.ConnectionCallbacks,
GooglePlayServicesClient.OnConnectionFailedListener{
	static LatLng LOC = new LatLng(53.558, 9.927);
//	static final LatLng KIEL = new LatLng(53.551, 9.993);
	private GoogleMap googleMap;
    private final static int
    CONNECTION_FAILURE_RESOLUTION_REQUEST = 9000;
    private LocationClient mLocationClient;
    private Location mCurrentLocation;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_map_coordinate);
		mLocationClient = new LocationClient(this, this, this);
	}
	
    @Override
    protected void onStart() {
        super.onStart();
        // Connect the client.
        mLocationClient.connect();
    }
    
    @Override
    protected void onStop() {
        // Disconnecting the client invalidates it.
        mLocationClient.disconnect();
        super.onStop();
    }

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_map_coordinate, menu);
		return true;
	}

	@Override
	public void onMapLongClick(LatLng arg0) {
		DeviceHelper.storeCoordinates(arg0.longitude, arg0.latitude);
		Toast.makeText(this, arg0.latitude + " " + arg0.longitude ,Toast.LENGTH_LONG)
		.show();	
		finish();
	}

	@Override
	public void onMapClick(LatLng arg0) {
		DeviceHelper.storeCoordinates(arg0.longitude, arg0.latitude);
		Toast.makeText(this, arg0.latitude + " " + arg0.longitude ,Toast.LENGTH_LONG)
		.show();	
		finish();
	}
	
	@SuppressLint("NewApi")
	private void initilizeMap() {
        if (googleMap == null) {
            googleMap = ((MapFragment) getFragmentManager().findFragmentById(
                    R.id.map)).getMap();
 
            googleMap.setOnMapClickListener(this);
            googleMap.setOnMapLongClickListener(this);
            googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(LOC, 15));
//            ImageLoaderGoogleMap imageLoaderGoogleMap = new ImageLoaderGoogleMap(this);
            
            // check if map is created successfully or not
            if (googleMap == null) {
                Toast.makeText(getApplicationContext(),
                        "Sorry! unable to create maps", Toast.LENGTH_SHORT)
                        .show();
            }
        }
    }

	@Override
	public void onConnectionFailed(ConnectionResult connectionResult) {
        if (connectionResult.hasResolution()) {
            try {
                // Start an Activity that tries to resolve the error
                connectionResult.startResolutionForResult(
                        this,
                        CONNECTION_FAILURE_RESOLUTION_REQUEST);
                /*
                 * Thrown if Google Play services canceled the original
                 * PendingIntent
                 */
            } catch (IntentSender.SendIntentException e) {
                // Log the error
                e.printStackTrace();
            }
        } else {
            /*
             * If no resolution is available, display a dialog to the
             * user with the error.
             */
//            showErrorDialog(connectionResult.getErrorCode());
        }
		
	}

	@Override
	public void onConnected(Bundle arg0) {
		mCurrentLocation = mLocationClient.getLastLocation();

		try {
			DeviceHelper.storeCoordinates(mCurrentLocation.getLongitude(), mCurrentLocation.getLatitude());
//			Toast.makeText(this, String.valueOf(mCurrentLocation.getLatitude()) + "," + String.valueOf(mCurrentLocation.getLongitude()) , Toast.LENGTH_LONG).show();
			LOC = new LatLng(mCurrentLocation.getLatitude(), mCurrentLocation.getLongitude());
		} catch(Exception e) {
//			Toast.makeText(this, "Please enable google service", Toast.LENGTH_SHORT).show();
//			alertbox("Google's Location Service", "Please enable your google service");
		}
		try {
			initilizeMap();
		} catch(Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public void onDisconnected() {
        Toast.makeText(this, "Disconnected. Please re-connect.",
                Toast.LENGTH_SHORT).show();
		
	}
}
